package com.wdf.rule;

import cn.hutool.json.JSONUtil;
import org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.utils.KieHelper;
import org.kie.memorycompiler.CompilationResult;
import org.kie.memorycompiler.JavaCompiler;
import org.kie.memorycompiler.JavaCompilerFactory;
import org.kie.memorycompiler.JavaConfiguration;

/**
 * @author wangdingfu
 * @date 2023-11-13 20:34:02
 */
public class RuleTest {

    private static final String sourceCode = "package com.wdf.rule.order;\n" +
            "\n" +
            "\n" +
            "/**\n" +
            " * @author wangdingfu\n" +
            " * @date 2023-11-09 17:44:44\n" +
            " */\n" +
            "public class OrderTest {\n" +
            "\n" +
            "\n" +
            "    /**\n" +
            "     * 订单金额\n" +
            "     */\n" +
            "    private int amount;\n" +
            "\n" +
            "    /**\n" +
            "     * 积分\n" +
            "     */\n" +
            "    private int score;\n" +
            "\n" +
            "    public int getAmount() {\n" +
            "        return amount;\n" +
            "    }\n" +
            "\n" +
            "    public void setAmount(int amount) {\n" +
            "        this.amount = amount;\n" +
            "    }\n" +
            "\n" +
            "    public int getScore() {\n" +
            "        return score;\n" +
            "    }\n" +
            "\n" +
            "    public void setScore(int score) {\n" +
            "        this.score = score;\n" +
            "    }\n" +
            "}\n";

    private static final String orderTestJson = "{\n" +
            "    \"amount\":150\n" +
            "}";

    private static final String ruleContent = "package rules\n" +
            "\n" +
            "import com.wdf.rule.order.OrderTest\n" +
            "import cn.hutool.json.JSONUtil\n" +
            "\n" +
            "\n" +
            "rule \"score_0\"\n" +
            "when\n" +
            "    $orderTest : OrderTest();\n" +
            "then\n" +
            "   System.out.println(JSONUtil.toJsonPrettyStr($orderTest));\n" +
            "end\n" +
            "//规则1：100元以下, 不加积分\n" +
            "rule \"score_1\"\n" +
            "when\n" +
            "    $s : OrderTest(amount <= 100)\n" +
            "then\n" +
            "    $s.setScore(0);\n" +
            "    System.out.println(\"消费100元以下, 不加积分 \");\n" +
            "end\n" +
            "\n" +
            "//规则2：100元-500元 加100积分\n" +
            "rule \"score_2\"\n" +
            "when\n" +
            "    $s : OrderTest(amount > 100 && amount <= 500)\n" +
            "then\n" +
            "    $s.setScore(100);\n" +
            "    System.out.println(\"消费100元-500元 加100积分 \");\n" +
            "end\n" +
            "\n" +
            "//规则3：500元-1000元 加500积分\n" +
            "rule \"score_3\"\n" +
            "when\n" +
            "    $s : OrderTest(amount > 500 && amount <= 1000)\n" +
            "then\n" +
            "    $s.setScore(500);\n" +
            "    System.out.println(\"消费500元-1000元 加500积分 \");\n" +
            "end\n" +
            "\n" +
            "//规则4：1000元 以上 加1000积分\n" +
            "rule \"score_4\"\n" +
            "when\n" +
            "    $s : OrderTest(amount > 1000)\n" +
            "then\n" +
            "    $s.setScore(1000);\n" +
            "    System.out.println(\"消费1000元 以上 加1000积分 \");\n" +
            "end\n";

    private static KieServices kServices = KieServices.Factory.get();

    private static void compire() {
        KnowledgeBuilderConfigurationImpl kconf = new KnowledgeBuilderConfigurationImpl();
        JavaConfiguration javaConf = (JavaConfiguration) kconf.getDialectConfiguration("java");
        JavaCompiler javaCompiler = JavaCompilerFactory.loadCompiler(javaConf);
//        CompilationResult res = javaCompiler.compile();
    }

    public static void main(String[] args) {
        Class<?> clazz = DynamicTest.orderTestClass();
        Object bean = JSONUtil.toBean(orderTestJson, clazz);

        KieFileSystem kieFileSystem = kServices.newKieFileSystem();
        kieFileSystem.write("src/main/java/com/wdf/rule/order/OrderTest.java", sourceCode);


        KieHelper helper = new KieHelper();
        helper.addContent(ruleContent, ResourceType.DRL);
        KieSession kSession = helper.build().newKieSession();
        kSession.insert(bean);
        kSession.fireAllRules();
        kSession.dispose();
        System.out.println(JSONUtil.toJsonPrettyStr(bean));
    }
}
